package cjq.demo.upload;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;





public class FileUpload extends HttpServlet {
	
	public static void main(String args[]){
		System.out.println(System.getProperty("user.dir"));
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		Date date = new Date();// ��ȡ��ǰʱ��
		SimpleDateFormat sdfFileName = new SimpleDateFormat("yyyyMMddHHmmss");
		// SimpleDateFormat sdfFolder = new SimpleDateFormat("yyMM");
		String newfileName = sdfFileName.format(date);// �ļ�����
		String fileRealPath = "";// �ļ������ʵ��ַ

		String firstFileName = "";

		// ����������ϴ��ļ������ڵ�����·�� ��������ڴ�ţ��������files\\��������� sdFolder+"\\"
		String savePath = this.getServletConfig().getServletContext()
				.getRealPath("/")
				+ "files\\";

		File file = new File(savePath);
		if (!file.isDirectory()) {
			file.mkdirs();
		}

		try {
			DiskFileItemFactory fac = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(fac);
			upload.setHeaderEncoding("UTF-8");
			// ��ȡ����ϴ��ļ�
			List fileList = fileList = upload.parseRequest(request);
			// �����ϴ��ļ�д�����
			Iterator it = fileList.iterator();
			while (it.hasNext()) {
				FileItem obit = (FileItem) it.next();
				// �������ͨ ������

				if (obit.isFormField()) { // ��ͨ��,��ȡҳ�����
					String field = obit.getFieldName();

					if (field.equals("value1")) {
						System.out.println(obit.getString("UTF-8"));

					} else if (field.equals("value2")) {

						System.out.println(obit.getString("UTF-8"));

					}
				}
				// ����� ��ý��

				if (obit instanceof DiskFileItem) {
					DiskFileItem item = (DiskFileItem) obit;
					// ���item���ļ��ϴ�����
					// ����ļ�����·��
					String fileName = item.getName();
					if (fileName != null) {
						firstFileName = item.getName().substring(
								item.getName().lastIndexOf("\\") + 1);
						String formatName = firstFileName
								.substring(firstFileName.lastIndexOf("."));// ��ȡ�ļ���׺��
						fileRealPath = savePath + newfileName + formatName;// �ļ������ʵ��ַ

						BufferedInputStream in = new BufferedInputStream(
								item.getInputStream());// ����ļ�������
						BufferedOutputStream outStream = new BufferedOutputStream(
								new FileOutputStream(new File(fileRealPath)));// ����ļ������
						Streams.copy(in, outStream, true);// ��ʼ���ļ�д����ָ�����ϴ��ļ���
						// �ϴ��ɹ���
						if (new File(fileRealPath).exists()) {

							System.out.println("�ϴ��ɹ��ˣ� ������������������");

							response.getWriter().write(
									fileRealPath.substring(fileRealPath
											.lastIndexOf("\\") + 1));
						}

					}
				}
			}
		} catch (org.apache.commons.fileupload.FileUploadException ex) {
			ex.printStackTrace();
			System.out.println("û���ϴ��ļ�");
			return;
		}
		/* response.getWriter().write("1"); */
	}

}
